﻿$(function () {
    $("#button-grid").datagrid({
        url: $.Api("Sys/Button/Grid"),
        nowrap: false, rownumbers: true, border: false,
        fit: true, singleSelect: true,
        pageSize: 20, pagination: true,
        idField: "ID", toolbar: "#button-tools",
        columns:
        [[
            {
                field: "Icon", title: "图标", width: 50, align: "center",
                formatter: function (v, r, i) { if (v) { return $.GetIcon(v); } }
            },
            { field: "Name", title: "名称", width: 100, align: "center" },
            { field: "IntSort", title: "排序", width: 50, align: "center" },
            { field: "Function", title: "方法", width: 100, align: "center" }
        ]]
    });
});

var Button = {
    //添加
    Insert: function () {
        Button.Mutual("添加", "sys/button/Edit.aspx");
    },
    //修改
    Update: function () {
        var row = $("#button-grid").datagrid("getSelected");
        if (row) {
            Button.Mutual("修改", "sys/button/Edit.aspx?id=" + row.ID);
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },
    //删除
    Delete: function () {
        var row = $("#button-grid").datagrid("getSelected");
        if (row) {
            $.EasyuiConfirm("确认删除?", function (del) {
                if (del) {
                    $.AjaxMask({
                        url: $.Api("Sys/Button/Delete"),
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            result = JSON.parse(result);
                            $.EasyuiSlide(result.Message);
                            if (result.Success) { Button.Reload(); }
                        }
                    });
                }
            });
        }
        else { $.EasyuiSlide(delnullrowmsg); }
    },
    //实体操作
    Mutual: function (title, href) {
        $.ModelDialog({
            title: title, href: href, width: 260,
            handler: function () {
                $.ModelForm({
                    validate: true, button: $(this),
                    id: "#button-model",
                    url: $.Api("Sys/Button/Save"),
                    success: function (result) { Button.Reload(); }
                });
            },
            onLoad: function () { Button.FormInit(); }
        });
    },
    //弹窗初始化
    FormInit: function () {
        $("#button-icon").plugintext({
            width: 150, required: true,
            iconCls: "icon-application_view_icons",
            onClick: function () {
                $("#button_iconopt").dialog("open");
            }
        });

        $("#button-type").combobox({
            editable: false, width: 150, panelHeight: "auto",
            data: [
                { value: "Purview", text: "权限标识" },
                { value: "Function", text: "功能按键" }
            ]
        });

        $("span.list-span", $("#button_iconopt")).click(function () {
            var value = $(this).attr("value");
            $("#button_iconopt").dialog("close");
            $("#button-icon").val(value);
            $("#button-icon").validatebox("validate");
        });
    },

    //排序向上
    Up: function () {
        Button.Order("up");
    },
    //排序向下
    Down: function () {
        Button.Order("down");
    },
    //排序操作
    Order: function (order) {
        var row = $("#button-grid").datagrid("getSelected");
        if (row) {
            $.AjaxMask({
                url: $.Api("Sys/Button/SetOrder"),
                type: "post", data: { id: row.ID, order: order },
                success: function (result) {
                    result = JSON.parse(result);
                    $.EasyuiSlide(result.Message);
                    if (result.Success) { Button.Reload(); }
                }
            });
        }
        else { $.EasyuiSlide(nullrowmsg); }
    },

    //刷新列表
    Reload: function () {
        $.DataGridReload("#button-grid");
    }
};